package com.sharp.authorization.service.admin.impl;

import com.github.pagehelper.PageInfo;
import com.sharp.authorization.dao.common.entity.AcRole;
import com.sharp.authorization.dao.common.entity.AcUser;
import com.sharp.authorization.dao.common.repository.AcUserRoleService;
import com.sharp.authorization.dao.common.repository.AcUserService;
import com.sharp.authorization.dao.join.repository.UserJoinRoleService;
import com.sharp.authorization.service.admin.AdminService;
import com.sharp.authorization.service.login.LoginService;
import com.sharp.authorization.vo.user.*;
import com.sharp.kernel.constant.err.ErrCodeSys;
import com.sharp.kernel.utils.DbUtil;
import com.sharp.kernel.utils.ReflectionUtil;
import com.sharp.framework.utils.security.MD5Util;
import com.sharp.kernel.validate.Validator;
import com.sharp.kernel.vo.query.QueryRespVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Title: AdminServiceImpl
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2020/3/30 16:19
 * @description Project Name: Mall
 * @Package: com.srct.service.account.service.impl
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AcUserService acUserService;
    @Resource
    private LoginService loginService;
    @Resource
    private UserJoinRoleService userJoinRoleService;
    @Resource
    private AcUserRoleService acUserRoleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUser(AddUserReq req) {
        Validator.assertNotBlank(req.getLoginId(), ErrCodeSys.PA_PARAM_NULL, "用户登录名");
        Validator.assertNull(acUserService.getUserByLoginId(req.getLoginId()), ErrCodeSys.PA_DATA_HAS_EXIST, "用户登录名");
        Validator.assertNotBlank(req.getPassword(), ErrCodeSys.PA_DATA_NOT_EXIST, "密码");
        AcUser user = loginService.createUser(req.getLoginId(), req.getPassword());
        AcUser updateUser = ReflectionUtil.copy(req, AcUser.class);
        updateUser.setId(user.getId());
        updateUser.setName(req.getUserName());
        updateUser.setPassword(user.getPassword());
        acUserService.updateByPrimaryKeySelective(updateUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUser(UpdateUserReq req) {
        Validator.assertNotBlank(req.getLoginId(), ErrCodeSys.PA_PARAM_NULL, "用户登录名");
        AcUser user = acUserService.getUserByLoginId(req.getLoginId());
        Validator.assertNotNull(user, ErrCodeSys.PA_DATA_NOT_EXIST, "用户登录名");
        AcUser updateUser = ReflectionUtil.copy(req, AcUser.class);
        updateUser.setId(user.getId());
        updateUser.setName(req.getUserName());
        if (StringUtils.isNotBlank(req.getPassword())) {
            Validator.assertTrue(StringUtils.equals(req.getPassword(), req.getPasswordConfirm()),
                    ErrCodeSys.PA_DATA_DIFF, "确认密码");
            updateUser.setPassword(MD5Util.generate(req.getPassword()));
            updateUser.setPasswordUpdateLastTime(new Date());
            updateUser.setPasswordErrorTimes(0);
        }
        acUserService.updateByPrimaryKeySelective(updateUser);
    }

    @Override
    public QueryRespVO<QueryUserRes> queryUser(QueryUserReq req) {
        PageInfo<AcUser> userList = acUserService.getUserByQueryUserReq(req);
        return new QueryRespVO<>(DbUtil.copy(userList, QueryUserRes.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cloneUserRole(CloneUserRoleReq req) {
        AcUser user = acUserService.getUserByLoginId(req.getLoginId());
        Validator.assertNotNull(user, ErrCodeSys.PA_DATA_NOT_EXIST, "原用户");
        AcUser targetUser = acUserService.getUserByLoginId(req.getTargetUserId());
        Validator.assertNotNull(targetUser, ErrCodeSys.PA_DATA_NOT_EXIST, "目标用户");
        List<AcRole> roleList = userJoinRoleService.getUserWithRoleListByLoginId(req.getLoginId()).getRoleList();
        List<String> roleIdList = ReflectionUtil.getFieldList(roleList, "id", String.class);
        acUserRoleService.deleteByUserId(targetUser.getId());
        acUserRoleService.bind(targetUser.getId(), roleIdList);
    }

}
